// pages/cart/cart.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:[] as any,
    cart:[] as any,
    totalPrice:0,
    totalNumber:0
  },

  // handelAddress(){
  //   console.log('handelAddress')
  //   wx.chooseAddress({
  //     success: (res)=> {
  //        const  address = res
  //       wx.setStorageSync('address',address)
  //       this.setData({
  //         address
  //       })
  //     }
  //   })
 
  // },
  // handItemchange(e:any){
  //   const goods_id = e.currentTarget.dataset.id
  //   console.log(goods_id)
  //   let {cart} = this.data
  //   let index = cart.findIndex((v:any)=>
  //     v.goods_id === goods_id
  //   )
  //   console.log('index',index)
  //  cart[index].checked = !cart[index].checked
  //  console.log(cart[index].checked)
  //   this.setCart(cart)
   
  // },
  // setCart(cart: any[]){
  //   let allcheck =true
  //   let totalPrice=0
  //    let totalNumber=0
  //    cart.forEach((v :any)=> {
  //     if(v.checked){
  //      totalPrice += v.num*v.goods_price
  //      totalNumber += v.num
  //     }else{
  //      allcheck =  false
  //     }
  //    });
  //    allcheck = cart.length!==0? allcheck:false
  //    this.setData({
  //      cart,
  //      allcheck,
  //      totalPrice,
  //      totalNumber
  //    })
  //    wx.setStorageSync('cart',cart)
  // },
  // handelAllchange(){
  //   let {cart} = this.data
  //   this.data.allcheck = !this.data.allcheck
  //   cart.forEach((v:any) => {v.checked = this.data.allcheck
  //   });

  //   this.setData({
  //     cart
  //   })
  //   this.setCart(cart)
  // },
  // 结算
  handelpay(){
    const {address,totalNumber} =this.data
    console.log(address)
    if(!address.userName){
      wx.showToast({
        title:'请选择收获地址',
        icon:'error'
      })
      return
    }
    if(totalNumber===0){
      wx.showToast({
        title:'您还没有选购商品',
        icon:'error'
      })
      return
    }
    wx.navigateTo({
      url:'/pages/pay/pay'
    })
  },
  handelOrderpay(){
    const token = wx.getStorageSync('token')
    if(!token){
      wx.navigateTo({
        url:'/pages/auth/auth'
      })
    }
  },
  /**
   * 
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
      const address = wx.getStorageSync('address')
      let cart = wx.getStorageSync('cart')||[]
    
     let totalPrice=0
      let totalNumber=0
        cart =  cart.filter((v:any)=>v.checked)
      cart.forEach((v :any)=> {
      
        totalPrice += v.num*v.goods_price
        totalNumber += v.num
     
      });
    
      this.setData({
        address ,
        cart,
      
        totalPrice,
        totalNumber
      })
      console.log('adress',this.data.address)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})